package com.org.shichui.controller;

import com.org.shichui.websocket.SocketServer;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class SocketController {

    @RequestMapping("sockettest")
    public String sockettest(){
        return "sockettest";
    }

    @RequestMapping("tongji")
    public String tongji(Model model){
        model.addAttribute("num", SocketServer.getOnlineNum());
        model.addAttribute("users", SocketServer.getOnlineUsers());
        return "tongji";
    }

    @RequestMapping("sendmsg")
    public String sendmsg(HttpServletRequest request){
        String username = request.getParameter("username");
        String msg = request.getParameter("msg");
        SocketServer.sendMessage(msg, username);
        return "success";
    }

    @RequestMapping("sendAll")
    public String sendAll(HttpServletRequest request){
        String msg = request.getParameter("msg");
        SocketServer.sendAll(msg);
        return "success";
    }
}
